package maiyatian.videolive.fragment.newest;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.extras.recyclerview.PullToRefreshRecyclerView;

import org.xutils.view.annotation.ContentView;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.IFragment;
import maiyatian.videolive.MainActivity;
import maiyatian.videolive.R;
import maiyatian.videolive.adapter.newest.NewestAdapter;
import maiyatian.videolive.bean.LiveBean;
import maiyatian.videolive.widget.GridSpacingItemDecoration;

/**
 * Created by 王中阳 on 2016/5/10.
 */
@ContentView(R.layout.fragment_newest)
public class NewestFragmentback extends IFragment implements NewestAdapter.IClickListener {
    public static final String TAG = "NewestFragment";
    MainActivity mycontext;

    List<LiveBean> datas;
    private NewestAdapter mAdapter;
    private RecyclerView recyclerView;
    private PullToRefreshRecyclerView mPullRefreshRecyclerView;

    public NewestFragmentback() {

    }

    public static NewestFragmentback newInstance() {
        NewestFragmentback fragment = new NewestFragmentback();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mycontext = (MainActivity) context;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updateUI();
    }

    private void updateUI() {
        mPullRefreshRecyclerView = (PullToRefreshRecyclerView) mycontext.findViewById(R.id.rvLiveLast);
        mPullRefreshRecyclerView.setMode(PullToRefreshBase.Mode.BOTH);
//        mPullRefreshRecyclerView.setHeaderLayout(new BaziHeaderLayout(mycontext));
        recyclerView = mPullRefreshRecyclerView.getRefreshableView();
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 10, true));
        mAdapter = new NewestAdapter(mycontext, this);
        datas = new ArrayList<>();
        recyclerView.setAdapter(mAdapter);
        //刷新数据的方法
        mPullRefreshRecyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                Log.e(TAG, "onPullDownToRefresh ");
                updateFromNet();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                Log.e(TAG, "onPullUpToRefresh ");
                PullUpFromNet();
            }


        });
        updateFromNet();
    }

    private void PullUpFromNet() {

    }

    private void updateFromNet() {
        for (int i = 0; i < 10; i++) {
            LiveBean liveBean = new LiveBean();
            datas.add(liveBean);
        }
        mAdapter.setMdatas(datas);

        mPullRefreshRecyclerView.onRefreshComplete();
    }

    @Override
    public void onItemClicked(int position) {

    }
}
